package com.tang.subject.infra.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.tang.common.enums.DeleteEnum;
import com.tang.subject.infra.basic.entity.pojo.SubjectMapping;
import com.tang.subject.infra.basic.mapper.SubjectMappingDao;
import com.tang.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 题目分类关系表(SubjectMapping)表服务实现类
 *
 * @author Tang
 * @since 2024-02-02 14:25:08
 */
@Slf4j
@Service("subjectMappingService")
public class SubjectMappingServiceImpl implements SubjectMappingService {
    @Resource
    private SubjectMappingDao subjectMappingDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SubjectMapping queryById(Long id) {
        return this.subjectMappingDao.queryById(id);
    }

    /**
     * 根据分类id查询标签信息
     *
     * @param subjectMapping 查询条件
     * @return java.util.List<java.lang.Long>
     * @author Tang
     * @date 2024/2/2 14:47
     **/
    @Override
    public List<SubjectMapping> queryLabel(SubjectMapping subjectMapping) {
        if (log.isInfoEnabled()) {
            log.info("SubjectMappingService.queryLabel.subjectMapping:{}", JSON.toJSONString(subjectMapping));
        }
        return this.subjectMappingDao.queryLabelGroupBy(subjectMapping);
    }

    /**
     * 新增数据
     *
     * @param subjectMapping 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectMapping insert(SubjectMapping subjectMapping) {
        setUnDeleted(subjectMapping);
        this.subjectMappingDao.insert(subjectMapping);
        return subjectMapping;
    }

    /**
     * 修改数据
     *
     * @param subjectMapping 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectMapping update(SubjectMapping subjectMapping) {
        this.subjectMappingDao.update(subjectMapping);
        return this.queryById(subjectMapping.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.subjectMappingDao.deleteById(id) > 0;
    }

    /**
     * 批量插入数据
     *
     * @param subjectMappingList 待插入的数据列表
     * @author Tang
     * @date 2024/2/4 14:47
     **/
    @Override
    public void insertBatch(List<SubjectMapping> subjectMappingList) {
        setUnDeleted(subjectMappingList);
        this.subjectMappingDao.insertBatch(subjectMappingList);
    }

    /**
     * 根据分类id批量查询
     *
     * @param categoryIds 分类id
     * @return java.util.List<com.tang.subject.infra.basic.entity.pojos.SubjectMapping>
     * @author Tang
     * @date 2024/2/20 13:28
     **/
    @Override
    public List<SubjectMapping> queryBatch(List<Long> categoryIds) {
        if (CollectionUtils.isEmpty(categoryIds)) {
            return Collections.emptyList();
        }
        return this.subjectMappingDao.queryLabelByCategoryIds(categoryIds);
    }

    private void setUnDeleted(SubjectMapping subjectMapping) {
        subjectMapping.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
    }

    private void setUnDeleted(List<SubjectMapping> subjectMappingList) {
        subjectMappingList.forEach(this::setUnDeleted);
    }

}
